import './login.scss'
import { NavBar,Button ,Toast } from 'antd-mobile'
import * as service from '../../api'
import React, { Component } from 'react';
import {withRouter} from 'react-router-dom'

class login extends Component {
  constructor(props){
    super(props);
    this.state={
      loginname:'',
      password:'',
    }
  }
  handleChange(e){
    this.setState({
      [e.target.name]:e.target.value
    })
  }
  async login(){
    var res = await service.user_login(this.state);
        // console.log(res);
        if (res.data.code == 200) {
         Toast.show({
          icon: 'success',
          content: '登录成功',
        })
         window.localStorage.setItem("token", res.data.data.token);
         window.localStorage.setItem("userid", res.data.data.userid); 
          this.props.history.push('index/home')
       } else if (res.data.code == 10010) {
         Toast.show({
          icon: 'fail',
          content: '该用户不存在',
        })
       } else if (res.data.code == 10011) {
        Toast.show({
          icon: 'fail',
          content: '密码错误',
        })
       } else {
        Toast.show({
          icon: 'fail',
          content: '异常错误',
        })
       }
  }
  tozhuce(){
    this.props.history.push('/zhuce')
  }
  onBack(){
    this.props.history.replace('/index/home')
  }
    render() {
        return (
            <div className='login'>
                 <NavBar onBack={this.onBack.bind(this)}  style={{
                    '--height': '50px',
                    '--border-bottom': '1px #eee solid',
                    }}>登录
                </NavBar>

                    <div className='shuru'>
                        <input type="text" placeholder='请输入手机号/账号/邮箱' value={this.state.loginname} onChange={this.handleChange.bind(this)} name='loginname'/>
                        <input type="password"  placeholder='请输入密码'  value={this.state.password} onChange={this.handleChange.bind(this)} name='password'/>
                    </div>
                        <Button block shape='rounded' color='danger'  size='large' onClick={this.login.bind(this)}>
                        登录
                      </Button>
                    <div className="tozhuce">
                      <p onClick={()=>{this.tozhuce()}}>手机快速注册</p>
                    </div>
                      
            </div>
        );
    }
}

export default withRouter(login);